package com.example.security.utils;

import com.example.base.dto.TokenUserDTO;
import com.example.security.assembly.UserPostLoginAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Objects;

public class SecurityUserUtil {

    public static TokenUserDTO getNowUser(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (Objects.isNull(authentication)){
            return null;
        }
        //PC 登陆的token转换
        if (authentication instanceof UserPostLoginAuthenticationToken){
            UserPostLoginAuthenticationToken token = (UserPostLoginAuthenticationToken)authentication;
            return token.getUser();
        }
        return null;
    }
}
